/**
 * 尊敬的用户，你好：页面 JS 面板是高阶用法，一般不建议普通用户使用，如需使用，请确定你具备研发背景，能够自我排查问题。
 * 我们可以用 JS 面板来开发一些定制度高功能，比如：调用API接口，自定义动作执行等等。
 */

// 当页面渲染完毕后马上调用下面的函数，这个函数是在当前页面 - 设置 - 生命周期 - 页面加载完成时中被关联的。
export function didMount() {
    console.log("「页面 JS」：当前页面地址 " + location.href);
    debugger
    let textEle = this.$$("text_6w18sg");

    addStyle();

    addHtml(textEle);
}

function addStyle() {
    const css = `
        .home-chart-container {
            overflow-y: auto;
            height: 100vh;
            background-size: 100% auto;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: transparent;
        }

        .home-recstat-title {
            width: 100%;
            height: 40px;
            line-height: 40px;
            background-color: transparent;
            color: white;
            text-align: center;
            font-size: 18px;
            margin-bottom: 20px;
        }

        .home-recstat-container {
            width: 100%;
            max-width: 1200px;
            height: 160px;
            display: flex;
            justify-content: center;
            align-items: center;
            background: transparent;
            padding: 0 10px;
        }

        .home-recstat-container-item {
            height: 100%;
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            color: white;
        }

        .splitline {
            width: 2px;
            height: 60px;
            background-color: rgba(120, 120, 120, 0.6);
            margin: 0 5px;
        }

        .home-stat-item {
            width: 100%;
            text-align: center;
        }

        .home-stat-item-title {
            font-size: 1rem;
            margin-bottom: 10px;
        }

        .home-stat-item-value {
            font-family: "lccd";
            font-size: 2.5rem;
            color: #FFA72E;
        }

        .item {
            width: 100%;
            padding: 0.2rem 0;
        }
    `;
    const style = document.createElement("style");
    style.id = "lawEnforcementOversight";
    style.textContent = css.trim();

    const existingStyle = document.head.querySelector("style#lawEnforcementOversight");
    if (existingStyle) {
        existingStyle.textContent = css.trim();
    } else {
        document.head.appendChild(style);
    }
}


function addHtml(textEle) {
    const str = `
    <div class="home-chart-container">
        <div class="home-recstat-title">执法监督</div>
        <div class="home-recstat-container" id="stat-bands"></div>
    </div>
  `;

    const targetNode = textEle.$el;
    const config = {childList: true, subtree: true};

    const observer = new MutationObserver((mutationsList, observer) => {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                initData();
                observer.disconnect(); // 完成初始化后断开观察
                break;
            }
        }
    });

    observer.observe(targetNode, config);
    textEle.$$setValue(str);
}

function initData() {
    const mockData = {
        statBands: [
            {title: "执法队员", value: 267},
            {title: "劝告程序", value: 2169},
            {title: "一般程序", value: 1456},
            {title: "简易程序", value: 1088}
        ]
    };

    function renderStatBands() {
        const container = document.getElementById("stat-bands");
        if (!container) return;

        container.innerHTML = "";
        mockData.statBands.forEach((item, index) => {
            const itemDiv = document.createElement("div");
            itemDiv.className = "home-recstat-container-item";
            itemDiv.innerHTML = `
        <div class="item">
            <div class="home-stat-item home-stat-item-title">${item.title}</div>
        </div>
        <div class="item">
            <div class="home-stat-item home-stat-item-value">${item.value}</div>
        </div>
      `;
            itemDiv.onclick = () => goToRecList(item.title);
            container.appendChild(itemDiv);

            if (index < mockData.statBands.length - 1) {
                const splitline = document.createElement("div");
                splitline.className = "splitline";
                container.appendChild(splitline);
            }
        });
    }

    function goToRecList(bizName) {
        alert(`Navigating to Rec List for ${bizName}`);
    }

    // 直接渲染数据
    renderStatBands();

    // 加载背景图
    const container = document.querySelector('.home-chart-container');
    if (container) {
        const img = new Image();
        img.src = "http://61.134.60.13:38080/linglong-api/files/iconfont/image/YmFja2dyb3VuZA.png";
        img.onload = () => {
            console.log("Background image loaded successfully!");
            container.style.backgroundImage = `url("${img.src}")`;
        };
        img.onerror = () => {
            console.error("Failed to load background image");
            container.style.backgroundColor = "gray";
            container.style.backgroundImage = "none";
            alert("背景图加载失败，请检查网络连接");
        };
    }
}
